/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Client;

import java.io.RandomAccessFile;
import java.io.File;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.net.*;
import javax.swing.*;
/**
 *
 * @author Siddharth Jain
 */
public class SubPartDownload implements Runnable{

    String fileNamePart;
    Integer partSize;
    Integer partSizeFinal;
    Socket clientSocket2;
    Integer check;
    Integer downloadOver;


    public SubPartDownload(String fileNamePart, Integer partSize, Integer partSizeFinal, Socket clientSocket2, Integer check){

        this.fileNamePart = fileNamePart;
        this.partSize = partSize;
        this.partSizeFinal = partSizeFinal;
        this.clientSocket2 = clientSocket2;
        this.check = check;
        this.downloadOver = 0;
    }

    public void run(){
        int current = 0;
        int bytesRead;
        try {
            byte [] mybytearray;

            if(check==8){
                mybytearray  = new byte [partSizeFinal];
            }
            else
                mybytearray  = new byte [partSize];
            InputStream is = clientSocket2.getInputStream();
            FileOutputStream fos = new FileOutputStream(fileNamePart);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            bytesRead = is.read(mybytearray,0,mybytearray.length);

            current = bytesRead;
            //String [] args = new String[1];
//            ClientProgressBar cpb = new ClientProgressBar(fileName);
//            String label = "Downloading "+fileName;
//            String label1 = "From: "+userName;
//            cpb.setMax(filesize);
//            cpb.setTitle(label);
//            cpb.setInfoLable(label, label1);

            //System.out.println("current "+current);
            //System.out.println("before while");

            do {
               bytesRead =
                  is.read(mybytearray, current, (mybytearray.length-current));
               //System.out.println("L "+bytesRead);
               //System.out.println("C "+current);
               if(bytesRead >= 0) {
                   //System.out.println("Length in if "+mybytearray.length);
                   current += bytesRead;
                   int value = current;
                   System.out.println(value);
                   //cpb.setValue(value);
               }

            } while(bytesRead > 0);
            System.out.println("Part "+check+" downloaded.");
            downloadOver = 1;
          //  }while(is.read()!= -2);
            //cpb.enableOpenButton();

            //System.out.println("after while");
            //System.out.println("Length again "+mybytearray.length);
            bos.write(mybytearray, 0 , current);

            //System.out.println("a while");
            bos.flush();
            //long end = System.currentTimeMillis();
           // System.out.println(end-start);
            bos.close();
            clientSocket2.close();
        }
        catch(Exception e){

                System.out.println(e);
        }
        }

    public int threadDownloaded(){
        return downloadOver;
    }
}
